<?php

namespace frontend\controllers;

use frontend\models\BindMobileForm;
use frontend\models\ReLoginForm;
use frontend\models\UserCompatibleModel;
use backend\models\User;
use Yii;
use frontend\components\Controller;
use frontend\models\LoginForm;
use frontend\models\SignupForm;
use frontend\models\ResetPasswordForm;

/**
 * 登录、注册、找回密码相关接口
 * PassportController
 */
class PassportController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            '*' => ['POST']
        ];
    }

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function rules() {
        return [
            [
                'actions' => ['login', 'register'],
                'allow' => true,
            ],
            [
                'allow' => true,
                'roles' => ['?'],
            ],
            [
                'actions' => ['logout', 'mine', 'bind-mobile', 'changepass'],
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * 帐号登录
     * @return mixed
     */
    public function actionLogin() {
        $model = new LoginForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入用户信息或者密码');
        }
        $model->token = $this->token;
        return $model->login();
    }

    /**
     * 新用户注册
     * @return mixed
     */
    public function actionRegister() {
        $model = new SignupForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入用户注册所需要的信息');
        }
        return $model->signup();
    }

    /**
     * 找回密码
     * @return mixed
     */
    public function actionForget() {
        $model = new ResetPasswordForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入用户注册所需要的信息');
        }
        return $model->signup();
    }

    /**
     * 退出登录
     */
    public function actionLogout() {
        $token = $this->token;
        if ($token->user_id == 0) {
            return parent::error('该用户已登出，退出登录失败');
        }
        $token->user_id = 0;
        return $token->save();
    }

    /**
     * 重新登录
     */
    public function actionMine() {
        $model = new ReLoginForm();
        $model->token = $this->token;
        return $model->reLogin();
    }

    /**
     * 绑定手机号
     */
    public function actionBindMobile() {
        $model = new BindMobileForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入手机号');
        }
        return $model->bind();
    }

    /**
     * 更改密码
     * @return null|static
     * @throws \yii\base\Exception
     * @throws \yii\web\HttpException
     */
    public function actionChangepass() {
        $post = Yii::$app->request->post();

        if (!isset($post['password']) || !isset($post['new_password'])) {
            parent::error('必须传入原始密码以及新密码');
        }

        if (strlen($post['new_password']) < 6 || strlen($post['new_password']) > 11) {
            parent::error('密码为6~11个字符');
        }

        $user = UserCompatibleModel::findOne(['id' => $this->getUser()->getId()]);
        if ($post['password'] == $post['new_password']) {
            parent::error('新密码与原密码重复');
        }

        if (!$user->validatePassword($post['password'])) {
            parent::error('原密码错误');
        }
        $user->setPassword($post['new_password']);
        if ($user->save()) {
            return true;
        }
        return false;
    }

}
